<?php
/**
 * (jobs) 备货国家品类/sku预估
 */
namespace App\Jobs\Stocking;

use App\Models\Basics\SysArea;
use App\Models\SsBasic\AreaClassifyEstimate;
use App\Models\SsBasic\AreaSkuEstimate;
use App\Models\SsBasic\CountryClassifyEstimate;
use App\Models\SsBasic\CountrySkuEstimate;
use App\Models\Stocking\StockingSku;
use App\Models\Stocking\StockingSkuRatio;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class AreaEuEstimateJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $estimate;

    /**
     * Estimate constructor.
     * @param $estimate
     */
    public function __construct($estimate)
    {
        $this->estimate = $estimate;
    }

    /**
     * Execute the job.
     * @return void
     */
    public function handle()
    {
        $this->exeFillData($this->estimate);
    }

    /**
     * 2.根据s_country_classify_estimate将所有国家的预估数据按品类汇总到s_area_classify_estimate
     * 2.1.根据s_country_sku_estimate将所有国家的预估数据按SKU汇总到s_area_sku_estimate
     * 2.2.更新备货产品的预估及比例（用站点SKU预估/站点品类预估）（s_stocking_sku）
     * @param $estimate
     * @return void
     */
    private function exeFillData($estimate){

        $areaId = SysArea::AREA_EU; //EU站点

        //EU国家的预估品类
        $classifyKey = [];
        foreach ($estimate as $_estimate){
            $classifyId = $_estimate->classify_id;
            if(!in_array($classifyId,$classifyKey)){
                $classifyKey[] = $classifyId;
            }
        }

        /******* 更新EU站点品类预估、EU站点SKU预估、EU站点SKU比例 start*********/
        foreach ($classifyKey as $_classifyId){
            //EU国家的品类预估(汇总))
            $countryClassifyEstimateSum = CountryClassifyEstimate::query()->selectRaw(getQueryEstimateStr())->where('classify_id',$_classifyId)->first();

            //2.1 更新EU站点的品类预估
            $classifyEstimate = AreaClassifyEstimate::query()->firstOrNew(['area_id'=>$areaId, 'classify_id'=>$_classifyId]);
            for($i = 0; $i < AreaSkuEstimate::ESTIMATE_NUM; $i++){
                $num = $countryClassifyEstimateSum->{'estimate_num'.$i} ?? 0; //赋值
                $classifyEstimate->{'estimate_num'.$i} = intval($num);
            }
            $classifyEstimate->save();

            //EU国家的sku预估(汇总)
            $countrySkuEstimates = CountrySkuEstimate::query()
                ->selectRaw('area_id,country_id,psku_id,classify_id,'.getQueryEstimateStr())
                ->where('area_id',$areaId)
                ->where('classify_id',$_classifyId)
                ->groupBy('psku_id')
                ->get();
            foreach ($countrySkuEstimates as $countrySkuEstimate){
                $areaPsku = $areaId.'_'.$countrySkuEstimate->psku_id; //站点+sku

                //2.2 更新EU站点的sku预估
                $skuEstimate = AreaSkuEstimate::query()->firstOrNew(['area_psku'=> $areaPsku]);
                //2.3 更新EU站点的备货sku
                $stockingSku = StockingSku::query()->firstOrNew(['area_psku'=> $areaPsku]);
                for($i = 0; $i < AreaSkuEstimate::ESTIMATE_NUM; $i++){
                    $num = $countrySkuEstimate->{'estimate_num'.$i} ?? 0; //赋值
                    $skuEstimate->{'estimate_num'.$i} = intval($num);
                    $stockingSku->{'estimate_num'.$i} = intval($num);
                }
                $skuEstimate->save();

                $stockingSku->area_id = $areaId;
                $stockingSku->psku_id = $countrySkuEstimate->psku_id;
                $stockingSku->classify_id = $countrySkuEstimate->classify_id;
                $stockingSku->save();
                $stockingSku->over_stock = StockingSku::getOverStock($stockingSku);
                $stockingSku->over_stock_value = StockingSku::getOverStockValue($stockingSku->over_stock, $stockingSku->unit_price);
                $stockingSku->save();

                //2.4 更新EU站点sku销售比例 (依据EU站点sku预估与EU站点品类预估,计算EU站点sku比例)
                $skuRatio = StockingSkuRatio::query()->firstOrNew(['area_psku'=> $areaPsku]);
                $classifyEstimateNum = 0;
                $skuEstimateNum = 0;
                for($i = 0; $i < AreaSkuEstimate::ESTIMATE_NUM; $i++){
                    $classifyEstimateNum = intval($classifyEstimate->{'estimate_num'.$i}) ?? 0; //指定参数(EU站点的品类预估数量)
                    $skuEstimateNum = intval($skuEstimate->{'estimate_num'.$i}) ?? 0; //指定参数(EU站点的sku预估数量)
                    if($classifyEstimateNum != 0 && $skuEstimateNum != 0){
                        break;
                    }
                }
                if($classifyEstimateNum != 0 && $skuEstimateNum != 0){
                    $stockingSkuRatio = sprintf("%.4f", $skuEstimateNum / $classifyEstimateNum);
                    $skuRatio->ratio = $stockingSkuRatio; //0.3001
                    $skuRatio->save();
                }
            }
        }
        /******* 更新EU站点品类预估、EU站点SKU预估、EU站点SKU比例   end*********/
    }

}

